﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Bzure.Common.Data;
using Bzure.Security.Permissions;

namespace Bzure.Apps.Security.DataAccess
{
    public class RoleRepository : RepositoryBase
    {
        static RoleRepository()
        {
            TeamRole entity = new TeamRole(Guid.NewGuid().ToString(), string.Empty);
            Type type = entity.GetType();
            AppendDataMap(type, "RoleID", () => entity.ID);
            AppendDataMap(type, "RoleName", () => entity.Name);
            AppendDataMap(type, "Description", () => entity.RoleDescription);
            ResultMapDelegate<TeamRole> queryMap = Role_Map;
            QueryMapContainer[entity.GetType()] = queryMap;
        }
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="contractProvider"></param>
        public RoleRepository(Bzure.Common.Transactions.ITransactionSupport contractProvider)
            : base(contractProvider)
        {

        }
        /// <summary>
        /// 实体对应数据库中的表
        /// </summary>
        public override string TableName
        {
            get
            {
                return "T_Security_Roles";
            }
        }
        /// <summary>
        /// 角色实体数据映射
        /// </summary>
        /// <param name="reader"></param>
        /// <returns></returns>
        public static TeamRole Role_Map(DataReader reader)
        {
            TeamRole role = new TeamRole(reader.GetString("RoleID"), reader.GetStringNullable("RoleName"))
            {
                RoleDescription = reader.GetString("Description")
            };
            return role;
        }
        ///// <summary>
        ///// 创建角色
        ///// </summary>
        ///// <param name="role"></param>
        ///// <returns></returns>
        //public bool CreateRole(TeamRole role)
        //{
        //    string sql = string.Format("INSERT INTO  {0}(RoleID,RoleName,Description,State) VALUES (@RoleID, @RoleName, @Description, @State)", this.TableName);

        //    return new EntityMap<string>(this.ContractProvider, sql)
        //         .AddGuid("RoleID", role.RoleID)
        //         .AddString("RoleName", role.RoleName)
        //         .AddString("Description", role.Description)
        //         .AddInt("State", role.State)
        //         .AsNonQuery() > 0;
        //}
        /// <summary>
        /// 获得所有角色
        /// </summary>
        /// <returns></returns>
        public TeamRole[] GetAllRoles()
        {
            string sql = string.Format("SELECT * FROM  {0}", this.TableName);
            return new SqlEntityMap<TeamRole>(this.ContractProvider, sql)
                .SetMap(Role_Map)
                .AsList().ToArray();
        }
      
    }
}
